//
//  MeTimetableVC.swift
//  UICPortal
//
//  Created by 高宇超 on 7/23/17.
//  Copyright © 2017 Yuchao. All rights reserved.
//

import UIKit

class MeTimetableVC: UIViewController, UIWebViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
    
    @IBOutlet weak var buttonsView: UIView!
    @IBOutlet weak var timetableCV: UICollectionView!
    
    
    var meVC: MeVC!
    var avStudent: AVStudent!
    var userRealm: UserRealm!
    var webViewYc: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        navigationItem.title = "Timetable"
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(MeTimetableVC.shareBtnPressed(_:)))
        tabBarController?.tabBar.isHidden = true
        
        // TODO: - retrieve timetable from webview
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        tabBarController?.tabBar.isHidden = true
        meVC.tableView.selectRow(at: IndexPath(row: 0, section: 1), animated: true, scrollPosition: .none)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        if self.isMovingToParentViewController {
            UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
        }
    }
    
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if UIDevice.current.orientation.isPortrait {
            buttonsView.frame.origin.y = portraitNavBarHeight
            timetableCV.frame.origin.y = portraitNavBarHeight + buttonsView.frame.height
        } else {
            buttonsView.frame.origin.y = landscapeNavBarHeight
            timetableCV.frame.origin.y = landscapeNavBarHeight + buttonsView.frame.height
        }
    }

    func canRotate(_ sender: Any) {}
    
    func shareBtnPressed(_ sender: Any) {
        
    }
    
    @IBAction func saveToAlbumBtnPressed(_ sender: Any) {
        
    }
    
    @IBAction func refreshBtnPressed(_ sender: Any) {
        _ = webViewYc.rtrvTimetableAndSave(userRealm)
    }
    
    @IBAction func saveChangesBtnPressed(_ sender: Any) {
        
    }
    
    // MARK: - WebView
    //
    func webViewDidStartLoad(_ webView: UIWebView) {
        
    }
    
    func webViewDidFinishLoad(_ webView: UIWebView) {
        if !webView.isLoading {
            
        }
    }
    
    

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let id = segue.identifier {
            switch id {
            case "fromMeVCToMeTimeTableVC":
                break
            default:
                break
            }
        }
    }

    override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
        
    }
    
    
    // MARK: - CollectionView
    //
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let row = indexPath.row
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "meTimetableCollectionViewCell", for: indexPath) as! MeTimetableCollectionViewCell
        cell.classNameLabel.text = "\(row)"
        cell.backgroundColor = .cyan
        return cell
    }
    
}
